package com.goldcup.controller;

import com.alibaba.fastjson.JSONObject;
import com.goldcup.common.ErrorCode;
import com.goldcup.common.ResponseData;
import com.goldcup.model.User;
import com.goldcup.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@Controller
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ResponseBody
    @PostMapping("/add")
    public ResponseData addUser(User user) {
        ResponseData responseData = new ResponseData();
        //验证用户是否重复
        if(userService.checkRepeat(user.getName())){
            responseData.setCode(ErrorCode.USER_REPEAT.getCode());
            responseData.setMsg(ErrorCode.USER_REPEAT.getMsg());
            return responseData;
        }
        User result= userService.addUser(user);
        if(null == result){
            return responseData.fail();
        }
        return responseData.success();
    }

    @ResponseBody
    @GetMapping("/all")
    public Object findAllUser(@RequestParam(name = "pageNum", required = false, defaultValue = "1") int pageNum, @RequestParam(name = "pageSize", required = false, defaultValue = "10") int pageSize) {
        return userService.findAllUser(pageNum, pageSize);
    }

    @ResponseBody
    @GetMapping("/get-name")
    public String getNameById(@RequestParam(name = "id", required = false, defaultValue = "1") int id){
        return userService.getUser(id).getName();
    }

    @ResponseBody
    @GetMapping("/login")
    public ResponseData login(@RequestParam(name = "name") String name,@RequestParam(name = "password") String password){
        ResponseData responseData = new ResponseData();
        return responseData;
    }
}


